package simplecrawler;

import java.net.*;
import threads.*;

public class Crawler implements MessageReceiver {

    public Crawler(String initLink, Queue q, int maxLevel, int maxThreads)
            throws InstantiationException, IllegalAccessException, MalformedURLException {

        AllowedRobotsURLList arul = new AllowedRobotsURLList(initLink);

        ThreadController tc = new ThreadController(SearcherThread.class,
                maxThreads,
                maxLevel,
                q,
                0,
                this,
                initLink,
                arul);
    }

    public void finishedAll() {
        System.out.println("Done! Finished crawling.");
    }

    public void receiveMessage(Object o, int threadId) {
        // In our case, the object is already string, but that doesn't matter
        System.out.println("[" + threadId + "] " + o.toString());
    }

    public void finished(int threadId) {
        System.out.println("[" + threadId + "] finished");
    }

    public static void main(String[] args) {
        try {
            int maxLevel = 2;
            int maxThreads = 10;
            int maxDoc = -1;
            if (args.length == 5) {
                maxThreads = Integer.parseInt(args[4]);
            }
            if (args.length >= 4) {
                maxDoc = Integer.parseInt(args[3]);
            }
            if (args.length >= 3) {
                maxLevel = Integer.parseInt(args[2]);
            }
            if (args.length >= 2) {
                URLQueue q = new URLQueue();
                q.setFilenamePrefix(args[1]);
                q.setMaxElements(maxDoc);
                q.push(new URL(args[0]), 0);
                Crawler myCrawler = new Crawler(args[0],q, maxLevel, maxThreads);
                return;
            }
        } catch (Exception e) {
            System.err.println("An error occured: "+e.toString());
        }

        System.err.println("Usage: java Crawler <url> <filenamePrefix> [<maxLevel> [<maxDoc> [<maxThreads>]]]");
        System.err.println("Crawls the web for jpeg pictures and mpeg, avi or wmv movies.");
        System.err.println("-1 for either maxLevel or maxDoc means 'unlimited'.");

    }
}
